﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECare.BUS.Data;
using ECare.Database;

namespace ECare.BUS
{
    public class CheckRecordsHelper
    {
        public static  bool CreateCheck(CheckInfo record)
        {
            using (var context = new Entities())
            {
                var newcheck = new check_records();
                newcheck.check_current_status = (int)record.Current_Status;
                newcheck.check_original_status = (int)record.Original_Status;
                newcheck.check_remark = record.Remark;

                newcheck.check_time = DateTime.Now;
                newcheck.check_type = (int)record.CheckType;

                newcheck.check_by = record.Check_By;

                newcheck.entity_id = record.EntityId;

                context.check_records.AddObject(newcheck);
                context.SaveChanges();

                return true;
            }
        }

        public static List<CheckInfo> GetCheckRecords(CheckType type, int entityId)
        {
            using (var context = new Entities())
            {
                var res = new List<CheckInfo>();

                int _type = (int)type;


                var records = context.check_records.Where(a => a.check_type == _type && a.entity_id ==entityId
                    
                    ).ToList();
                foreach (var record in records)
                {
                    var newrecord = new CheckInfo();

                    newrecord.Check_By = record.check_by.HasValue?record.check_by.Value:0;
                    newrecord.Check_Time = record.check_time.Value;
                    newrecord.Current_Status = record.check_current_status.Value;
                    newrecord.Original_Status = record.check_original_status.Value;
                    newrecord.Remark = record.check_remark;
                    res.Add(newrecord);
                }

                return res;
            }
        }
    }
}
